package Concurrent;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description：
 * Author: zhangc
 * Date：2017/9/23 17:05
 */
public class MyCyclicBarrier {

    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
    private static AtomicInteger integer = new AtomicInteger(0);
    private static ExecutorService executorService = Executors.newFixedThreadPool(11);

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            repeat();
        }
        executorService.shutdown();
    }

    public static void repeat() {
        for (int i = 0; i < 10; i++) {
            executorService.execute(new Th());
        }
    }

    static class Th implements Runnable {
        @Override
        public void run() {
            try {
                cyclicBarrier.await();
                System.out.println(integer.getAndIncrement());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

}
